<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 先检查并删除旧表（按依赖关系倒序删除）
        Schema::dropIfExists('role_function_permissions');
        Schema::dropIfExists('function_permissions');
        Schema::dropIfExists('permission_nodes');
        Schema::dropIfExists('functions');
        Schema::dropIfExists('menus');

        // 重新创建菜单表（简化版，去掉树形结构）
        Schema::create('menus', function (Blueprint $table) {
            $table->id();
            $table->string('code', 100)->unique()->comment('菜单编码');
            $table->string('name', 100)->comment('菜单名称');
            $table->string('path', 200)->nullable()->comment('前端路由路径');
            $table->string('component', 100)->nullable()->comment('前端组件名');
            $table->string('icon', 50)->nullable()->comment('菜单图标');
            $table->text('description')->nullable()->comment('菜单描述');
            $table->enum('status', ['active', 'inactive'])->default('active')->comment('菜单状态');
            $table->boolean('is_visible')->default(true)->comment('是否可见');
            $table->integer('sort_order')->default(0)->comment('排序权重');
            $table->timestamps();

            $table->index(['code', 'status']);
            $table->index(['status', 'is_visible']);
            $table->index('sort_order');
        });

        // 创建功能表
        Schema::create('functions', function (Blueprint $table) {
            $table->id();
            $table->string('code', 100)->unique()->comment('功能编码，前端用作权限判断标识');
            $table->string('name', 100)->comment('功能名称');
            $table->enum('level', ['core', 'optional'])->default('optional')->comment('功能级别');
            $table->unsignedBigInteger('menu_id')->comment('所属菜单ID');
            $table->text('description')->nullable()->comment('功能描述');
            $table->json('dependencies')->nullable()->comment('功能依赖的其他功能ID列表');
            $table->enum('status', ['active', 'inactive'])->default('active')->comment('功能状态');
            $table->integer('sort_order')->default(0)->comment('排序权重');
            $table->timestamps();

            $table->foreign('menu_id')->references('id')->on('menus')->onDelete('cascade');
            $table->index(['menu_id', 'level']);
            $table->index(['code', 'status']);
        });

        // 创建权限节点表
        Schema::create('permission_nodes', function (Blueprint $table) {
            $table->id();
            $table->string('code', 150)->unique()->comment('权限节点编码');
            $table->string('name', 100)->comment('权限节点名称');
            $table->enum('type', ['page', 'api', 'field'])->comment('权限类型');
            $table->string('resource', 200)->nullable()->comment('关联资源');
            $table->text('description')->nullable()->comment('权限描述');
            $table->enum('status', ['active', 'inactive'])->default('active')->comment('权限状态');
            $table->timestamps();

            $table->index(['code', 'status']);
            $table->index(['type', 'status']);
        });

        // 创建功能权限关联表
        Schema::create('function_permissions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('function_id')->comment('功能ID');
            $table->unsignedBigInteger('permission_node_id')->comment('权限节点ID');
            $table->timestamps();

            $table->foreign('function_id')->references('id')->on('functions')->onDelete('cascade');
            $table->foreign('permission_node_id')->references('id')->on('permission_nodes')->onDelete('cascade');
            $table->unique(['function_id', 'permission_node_id'], 'unique_function_permission');
        });

        // 创建角色功能权限表
        Schema::create('role_function_permissions', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('role_id')->comment('角色ID');
            $table->unsignedBigInteger('function_id')->comment('功能ID');
            $table->boolean('auto_granted')->default(false)->comment('是否自动授权');
            $table->unsignedBigInteger('granted_by')->nullable()->comment('授权人ID');
            $table->timestamp('granted_at')->nullable()->comment('授权时间');
            $table->timestamps();

            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->foreign('function_id')->references('id')->on('functions')->onDelete('cascade');
            $table->unique(['role_id', 'function_id'], 'unique_role_function');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('role_function_permissions');
        Schema::dropIfExists('function_permissions');
        Schema::dropIfExists('permission_nodes');
        Schema::dropIfExists('functions');
        Schema::dropIfExists('menus');
    }
};
